{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "demographic-young",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "raw",
   "id": "extra-spain",
   "metadata": {},
   "source": [
    "四个方法： dumps、dump、loads、load。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "indian-secretary",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"xiaojun\": \"123456\", \"xiaohei\": \"7891\", \"abc\": \"11111\"}\n"
     ]
    }
   ],
   "source": [
    "stus = {'xiaojun':'123456','xiaohei':'7891','abc':'11111'}\n",
    "res2 = json.dumps(stus)   #先把字典转成json\n",
    "print(res2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "radical-packet",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(res2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "european-chart",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "test = {\"username\":\"测试\",\"age\":16}\n",
    "print (type(test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "mathematical-income",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"username\": \"测试\", \"age\": 16}\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "\n",
    "python_to_json = json.dumps(test,ensure_ascii=False)\n",
    "print (python_to_json)\n",
    "print (type(python_to_json))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "united-blend",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "测试\n"
     ]
    }
   ],
   "source": [
    "# 将json对象转换成python对象\n",
    "json_to_python = json.loads(python_to_json)\n",
    "print (type(json_to_python))\n",
    "print (json_to_python['username'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "anonymous-furniture",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
